package com.zhijiang.common.util;

import cn.hutool.json.JSONUtil;


import com.zhijiang.hospital.entrty.Repair;
import com.zhijiang.hospital.service.RepairService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Executors;

/**
 * 定时任务的使用
 * @author fan
 **/
@Component("Task")
@EnableScheduling
@EnableAsync
@Slf4j
public class Task  implements ApplicationContextAware {


    /**
     * 异步线程池设置
     * 如果所有的任务都使用异步，线程池执行的话，可以不配置同步任务线程池。但是使用异步需要注意的是异步线程池的大小配置。异步的线程池默认是无上限的开启线程数的。
     * @return
     */
    @Bean
    SimpleAsyncTaskExecutor simpleAsyncTaskExecutor(){
        SimpleAsyncTaskExecutor simpleAsyncTaskExecutor = new SimpleAsyncTaskExecutor();
        simpleAsyncTaskExecutor.setConcurrencyLimit(7);		//设置最大并行数
        simpleAsyncTaskExecutor.setDaemon(true); 	//设置为守护线程
        return  simpleAsyncTaskExecutor;
    }

    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Resource
    RepairService repairService;
//    @Scheduled(cron="0 */1 * * * ? ")   //每5秒执行一次
    public void user() throws Exception {
        System.out.println(new Date());
        Date start = new Date();start.setHours(0);start.setMinutes(0); start.setSeconds(0);
        Date end = new Date();end.setHours(23);end.setMinutes(59); end.setSeconds(59);
        SimpleDateFormat toDay = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Repair> repairs  = repairService.selectByToDateRepairAll(sdf.format(start),sdf.format(end));
//
        repairs.forEach(s->stringRedisTemplate.opsForList().leftPush("weixiu", JSONUtil.toJsonPrettyStr(s)));
        repairService.automaticDispatch(repairs,sdf.format(start),sdf.format(end));

    }

    public void weixiu() throws Exception {
   repairService.updateAuto();
    }
//    /**
//     * 每五秒执行一次
//     */
//    @Scheduled(cron = "*/5 * * * * ?")
//    private void printNowDate() {
//        long nowDateTime = System.currentTimeMillis();
//        System.out.println("固定定时任务执行:--->"+nowDateTime+"，此任务为每五秒执行一次");
//    }
@Scheduled(cron="0 */1 * * * ? ")
public void  tast1(){

    //发短信     未完成任务提醒

    }

    private static ApplicationContext context;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }
    public static ApplicationContext getApplicationContext() { return context;}
    public static Object getBean(String name) {return getApplicationContext().getBean(name); }
}
